// ==UserScript==
// @name         谷歌搜索设置-搜索时打开新标签页
// @namespace    https://www.google.com/search
// @version      1.2
// @description  在谷歌搜索结果中点击链接时在新标签页打开
// @author       DeepSeek
// @match        https://www.google.com/search?*
// @icon         https://gitee.com/selling-surprises/Greasyfork-Script-repository/raw/main/%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E/%E8%B0%B7%E6%AD%8C%E6%90%9C%E7%B4%A2%E8%AE%BE%E7%BD%AE-%E6%90%9C%E7%B4%A2%E6%97%B6%E6%89%93%E5%BC%80%E6%96%B0%E6%A0%87%E7%AD%BE%E9%A1%B5/googleg-icon.png
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    // 目标链接选择器
    const LINK_SELECTORS = [
        'a[jsname="UWckNb"]', // 普通搜索结果链接
        'a[jsname="qOi31d"]'  // 特殊结果链接（视频、新闻等）
    ];

    // 修改链接目标为在新标签页打开
    function modifyLinks() {
        const links = document.querySelectorAll(LINK_SELECTORS.join(', '));
        links.forEach(link => {
            if (link.target !== '_blank') {
                link.target = '_blank';
                // 添加视觉反馈
                link.style.transition = 'background-color 0.3s';
                link.addEventListener('mouseover', () => {
                    link.style.backgroundColor = 'rgba(66, 133, 244, 0.1)';
                });
                link.addEventListener('mouseout', () => {
                    link.style.backgroundColor = '';
                });
            }
        });
    }

    // 处理滚动加载
    function handleInfiniteScroll() {
        const observer = new MutationObserver(mutations => {
            let shouldUpdate = false;
            mutations.forEach(mutation => {
                if (mutation.addedNodes.length) {
                    shouldUpdate = true;
                }
            });
            if (shouldUpdate) {
                modifyLinks();
            }
        });

        observer.observe(document.documentElement, {
            childList: true,
            subtree: true,
            attributes: false,
            characterData: false
        });
    }

    // 添加样式
    function addStyles() {
        const style = document.createElement('style');
        style.innerHTML = `
            .g-new-tab-indicator {
                display: inline-block;
                margin-left: 5px;
                font-size: 10px;
                color: #4285f4;
                vertical-align: super;
            }
        `;
        document.head.appendChild(style);
    }

    // 添加状态提示
    function addStatusIndicator() {
        const indicator = document.createElement('div');
        indicator.innerHTML = `
            <div style="position: fixed; bottom: 20px; right: 20px; background: #fff; padding: 10px 15px; border-radius: 4px; box-shadow: 0 2px 10px rgba(0,0,0,0.2); z-index: 9999; font-size: 14px; color: #4285f4; border-left: 3px solid #4285f4;">
                <strong>Google 新标签页脚本已启用</strong>
                <div style="font-size: 12px; color: #5f6368; margin-top: 5px;">所有搜索结果将在新标签页中打开</div>
            </div>
        `;
        document.body.appendChild(indicator);

        // 5秒后淡出
        setTimeout(() => {
            indicator.style.opacity = '0';
            indicator.style.transition = 'opacity 1s';
            setTimeout(() => {
                indicator.remove();
            }, 1000);
        }, 5000);
    }

    // 初始化
    function init() {
        addStyles();
        modifyLinks();
        handleInfiniteScroll();
        addStatusIndicator();

        // 添加标题提示
        document.title = document.title.replace('Google 搜索', 'Google 搜索 (新标签页)');
    }

    // 等待页面稳定后执行
    if (document.readyState === 'complete') {
        init();
    } else {
        window.addEventListener('load', init);
    }
})();